package com.qf.dao.daoimpl;

import com.qf.dao.OrderDao;
import com.qf.pojo.Order;
import com.qf.pojo.OrderItem;
import com.qf.pojo.Product;
import com.qf.utils.dbutilUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * @author Mr.Liu
 * @date 2021/11/27 20:09
 */

public class OrderDaoImpl implements OrderDao {
    @Override
    public int addOrder(Order order) throws SQLException {
        QueryRunner qr = new QueryRunner();
        String sql = "insert into orders values(?,?,?,?,?,?,?,?);";
        Connection connection = dbutilUtils.getConnection();
        System.out.println(connection);
        int i = qr.update(connection,
                sql,
                order.getOid(),
                order.getOrdertime(),
                order.getTotal(),
                order.getState(), order.getAddress(),
                order.getName(),
                order.getTelephone(),
                order.getUser().getUid());
        System.out.println(i);
        return i ;
    }

    @Override
    public void addOrderItem(OrderItem orderItem) throws SQLException {
        QueryRunner qr = new QueryRunner();
        String sql = "insert into orderitem values(?,?,?,?,?);";
        int i = qr.update(dbutilUtils.getConnection(), sql, orderItem.getItemid(), orderItem.getCount(), orderItem.getSubtatal(), orderItem.getProduct().getPid(), orderItem.getOrder().getOid());
        System.out.println(i);
    }

    /**
     * 查询总条数
     * @param uid
     * @return
     */
    @Override
    public int foundTotalSize(String uid) throws SQLException {
        QueryRunner qr = new QueryRunner(dbutilUtils.getDataSource());
        String sql = "select count(oid) from orders where uid = ?;";
        Object tSize = qr.query(sql, new ScalarHandler<>(), uid);
        String s = String.valueOf(tSize);
        int totalSize = Integer.parseInt(s);
        System.out.println(totalSize);
        return totalSize;
    }

    /**
     * 查询订单集合
     * @param currentPage
     * @param pageSize
     * @param uid
     * @return
     */
    @Override
    public List<Order> foundOrderByPage(int currentPage, int pageSize, String uid) throws SQLException, InvocationTargetException, IllegalAccessException {
        QueryRunner qr = new QueryRunner(dbutilUtils.getDataSource());
        String sql = "select * from orders where uid = ? limit ?,?;";
        List<Order> listOrder = qr.query(sql, new BeanListHandler<Order>(Order.class), uid, (currentPage-1)*pageSize, pageSize);
        for(Order order : listOrder){
            String sql1 = "select * from orderitem o,product p where o.`pid` = p.`pid` and oid =?;";
            List<Map<String, Object>> obj = qr.query(sql1, new MapListHandler(), order.getOid());
            for(Map<String, Object> o : obj){
                //封装商品
                Product p = new Product();
                BeanUtils.populate(p,o);
                //封装订单项
                OrderItem oi = new OrderItem();
                BeanUtils.populate(oi,o);
                oi.setProduct(p);//将商品封装单订单项里
                order.getOrderItems().add(oi);//将订单项添加到订单中
            }
        }
        return listOrder;
    }
}
